////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//		File Name		:		CProjectile.cpp
//
//		Author			:		Lane Shapiro
//
//		Purpose			:		All functions and members of the CProjectile class
//
//		Dates			:		Written( April 2, 2011 ) by Lane Shapiro
//	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "CProjectile.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../Game States/CGamePlayState.h"
#include "../Core/MessageSystem.h"
#include "../Game Objects/CEnemy.h"
#include "../managers/VIN_SoundManager.h"

CProjectile::CProjectile()
{
	SetWidth(16);
	SetHeight(16);

	colide = true;

	projectileScale = 1.0f;
}

CProjectile::~CProjectile()
{

}

void CProjectile::Render()
{
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
	pTM->Draw( GetImageID(), (int)GetPosX(), (int)GetPosY(), projectileScale, projectileScale );
}

void CProjectile::Update(float fElapsedTime)
{
	SetVelY(GetVelY() - (-100.0f * fElapsedTime));

	SetPosX( GetPosX() + ( GetVelX() * fElapsedTime ));
	SetPosY( GetPosY() + ( GetVelY() * fElapsedTime ));

	if(GetPosX() < 0 || GetPosY() > 600)
	{
		MessageSystem::GetInstance()->SendNewMessage(new CDestroyObjectMessage(this, 2));
	}
}

RECT CProjectile::GetRect()
{
	RECT CollisionRect;
	SetRect(&CollisionRect, (int)GetPosX(), (int)GetPosY(), (int)(GetPosX() + GetWidth()), (int)(GetPosY() + GetHeight()));
	return CollisionRect;
}

bool CProjectile::CheckCollision( IBaseInterface* pBase)
{
	if(!colide)
		return false;

	RECT TempRect;

	if(pBase->GetType() == OBJ_ENEMY)
	{
		CBaseEnemy* enemy = (CBaseEnemy*)pBase;

		if(IntersectRect(&TempRect, &GetRect(), &enemy->GetHealthRect()))
		{
			if(enemy->GetFriendly() == false)
			{
				enemy->hurtcolor();
				enemy->SetHP(enemy->GetHP() - .8f * CGamePlayState::GetInstance()->GetPlayer()->GetCastleLevel());
				VIN_SoundManager::GetInstance()->PlayHurt();

				MessageSystem::GetInstance()->SendNewMessage(new CDestroyObjectMessage(this, 2));
				MessageSystem::GetInstance()->SendNewMessage(new CCreateParticleEmitterMessage(enemy->GetPosX() + 12, enemy->GetPosY() - 16, 0, 0, 0));
				return true;
			}
		}
	}
	else if(pBase->GetType() == OBJ_PLAYER)
	{
		CPlayer* player = (CPlayer*)pBase;
		
		if(IntersectRect(&TempRect, &GetRect(), &player->GetHealthRect()))
		{
			MessageSystem::GetInstance()->SendNewMessage(new CDestroyObjectMessage(this, 2));
		}
	}

	return false;
}